
package com.contactmodule.servlet;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import util.UtilDAO;

import com.companymodule.bean.TbCompany;
import com.companymodule.bean.TbCompanyDepartMiddle;
import com.companymodule.dao.TbCompanyDAO;
import com.companymodule.dao.TbCompanyDepartMiddleDAO;
import com.contactmodule.bean.TbContact;
import com.contactmodule.dao.TbContactDAO;
import com.usersmodule.bean.TbUsers;

public class TbContactServlet extends HttpServlet{
    public void processRequest(HttpServletRequest request,HttpServletResponse response) throws ServletException, IOException {
        String method = request.getParameter("method")==null?"":request.getParameter("method");
        int userId=((TbUsers)request.getSession().getAttribute("Users")).getUserId();
        if("add".equals(method)){
        String contactName=request.getParameter("contactName");
        int departId=0;
        int companyId=0;
        String contactSex=request.getParameter("contactSex");
        String contactPhone=request.getParameter("contactPhone");
        String contactEmail=request.getParameter("contactEmail");
        String contactBirthday=request.getParameter("contactBirthday");
        String contactAddress=request.getParameter("contactAddress");
        int contactState=Integer.parseInt(request.getParameter("contactState"));
        String parDepartId=request.getParameter("parDepartId");
        if(parDepartId.startsWith("C")){//表示是所新建的联系人是直属公司下的。
        	String companyIdStr = parDepartId.substring(1);
        	companyIdStr = companyIdStr.substring(0,companyIdStr.length()-5);
        	companyId = Integer.parseInt(companyIdStr);
        }else if(parDepartId.startsWith("D")){//新建联系人是属于某个组下的、
        	String departIdStr = parDepartId.substring(1);
        	departIdStr = departIdStr.substring(0, departIdStr.length()-5);
        	departId = Integer.parseInt(departIdStr);
        	TbCompanyDepartMiddle tbCompanyDepart = (new TbCompanyDepartMiddleDAO().getCompanyDepartListByDepartId(departId).get(0));
     		TbCompany tbCompany = new TbCompanyDAO().getTbCompanyByCompanyId(tbCompanyDepart.getCompanyId());
     		companyId = tbCompany.getCompanyId();
        }
            if(new TbContactDAO().add(new TbContact(contactName,companyId,departId,contactSex,contactPhone,contactEmail,contactBirthday,contactAddress,contactState,userId))){
            request.getSession().setAttribute("result", "添加成功");
            }else{            
            request.getSession().setAttribute("result", "添加失败");
            }
            goList(request, response);
        }
        if("del".equals(method)){
        	TbContact contact = new TbContactDAO().getTbContactByContactId(Integer.parseInt(request.getParameter("delIds")));
            int companyId = contact.getCompanyId();
            int departId = contact.getDepartId();
            if(UtilDAO.delin("Tb_Contact", "ContactId", request.getParameter("delIds"))){            
            	request.getSession().setAttribute("result", "删除成功");
            }else{            
            	request.getSession().setAttribute("result", "删除失败");
            }
            	List<TbContact> contactList =new ArrayList<TbContact>(); 
            	if(departId==0){
            		String condition=" CompanyId="+companyId;
            		contactList = new TbContactDAO().getContactList(condition);
            	}else {//部门
            		String condition=" DepartId="+departId;
            		contactList = new TbContactDAO().getContactList(condition);
            	}
              
            goList(request, response);
        }
        if("edit".equals(method)){
        int contactId=Integer.parseInt(request.getParameter("contactId"));
        System.out.println("contactId="+contactId);
        String contactName=request.getParameter("contactName");
        String contactSex=request.getParameter("contactSex");
        String contactPhone=request.getParameter("contactPhone");
        String contactEmail=request.getParameter("contactEmail");
        String contactBirthday=request.getParameter("contactBirthday");
        String contactAddress=request.getParameter("contactAddress");
        int contactState=0;//Integer.parseInt(request.getParameter("contactState"));
        TbContact contact = new TbContactDAO().getTbContactByContactId(contactId);
        int companyId = contact.getCompanyId();
        int departId = contact.getDepartId();
            if(new TbContactDAO().edit(new TbContact(contactId,contactName,contactSex,contactPhone,contactEmail,contactBirthday,contactAddress,contactState,userId))){
            request.getSession().setAttribute("result", "修改成功");
            }else{            
            request.getSession().setAttribute("result", "修改失败");
            }
        	List<TbContact> contactList =new ArrayList<TbContact>(); 
        	if(departId==0){
        		String condition=" CompanyId="+companyId;
        		contactList = new TbContactDAO().getContactList(condition);
        	}else {//部门
        		String condition=" DepartId="+departId;
        		contactList = new TbContactDAO().getContactList(condition);
        	}
        	goList(request, response);
        }
        if ("list".equals(method)) goList(request, response);
        if ("listById".equals(method)) {
             request.getSession(true).setAttribute("tbContact",new TbContactDAO().getTbContactByContactId(Integer.parseInt(request.getParameter("contactId"))));
            response.sendRedirect("ContactModule/TbContactOperate.jsp");
    	}
        if("showadd".equals(method)){
            String parDepartId = request.getParameter("parDepartId");//get the parentId of the depart
            request.getSession(true).removeAttribute("tbContact");
            request.getSession().setAttribute("parDepartId", parDepartId);
            response.sendRedirect("ContactModule/TbContactOperate.jsp");
        }
        if("contactListById".equals(method)){//得到公司下的直属联系人
        	String typeId=request.getParameter("typeId");//区分是公司直属联系人还是部门中的联系人
        	String id = request.getParameter("id");//公司或部门的ID 
        	System.out.println("typeId=="+typeId+"&&id=="+id);
        	List<TbContact> contactList =new ArrayList<TbContact>(); 
        	if(typeId.equals("0")){//公司
        		String condition=" CompanyId="+id;
        		contactList = new TbContactDAO().getContactList(condition);
        	}else if(typeId.equals("1")){//部门
        		String condition=" DepartId="+id;
        		contactList = new TbContactDAO().getContactList(condition);
        	}
        	String typeName=typeId.equals("0")?"C":typeId.equals("1")?"D":"";
        	String parDepartId="";
        	if(typeName.equals("C")){
        		parDepartId=typeName+id+"90000";
        	}else if(typeName.equals("D")){
        		parDepartId=typeName+id+"80000";
        	}
        	request.getSession().removeAttribute("tbContactList");
        	request.getSession().setAttribute("tbContactList", contactList);
        	request.getSession().setAttribute("parDepartId",parDepartId);
        	response.sendRedirect("ContactModule/ContactList.jsp");
        }
    }
    public void goList(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
    	request.getSession().setAttribute("result", new TbContactDAO().getLastResult());
        response.sendRedirect("ContactModule/TbContactList.jsp");
    }
    public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doGet
    public void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        this.processRequest(request, response);
    }// end method doPost
}
